package GUILayer;

import java.awt.EventQueue;
import ModelLayer.*;
import CtrLayer.*;

import javax.swing.AbstractButton;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
import java.awt.Color;
import javax.swing.border.TitledBorder;
import javax.swing.UIManager;
import java.awt.SystemColor;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class SaleGUI {

	private JFrame saleWindow;
	private OrderCtr orderCtr;
	private Employee employee;
	private JTextField customerIDField;
	private JTextField phoneNumberField;
	private JTextField productIDField;
	private JTextField productNameField;
	private JTextField amountField;
	private Customer customer;
	private Order order;
	private JTextField customerNameField;
	private PersonCtr personCtr;
	private JTextField priceField;
	private JTextField totalField;
	private MainGUI mainGUI;
	private ReceiptGUI receiptGUI;

	/**
	 * Launch the application.
	 */
	

	/**
	 * Create the application.
	 */
	public SaleGUI(Employee employee) {
		
		this.employee = employee;
		orderCtr = orderCtr.getInstance();
		personCtr = personCtr.getInstance();
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		saleWindow = new JFrame();
		saleWindow.setTitle("Salgsmenu");
		saleWindow.setBounds(100, 100, 387, 497);
		saleWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JMenuBar menuBar = new JMenuBar();
		saleWindow.setJMenuBar(menuBar);
		
		JMenu FilesMenu = new JMenu("Filer");
		menuBar.add(FilesMenu);
		
		JMenuItem endProgramOption = new JMenuItem("Luk program");
		FilesMenu.add(endProgramOption);
		
		JMenu helpMenu = new JMenu("Hj\u00E6lp");
		menuBar.add(helpMenu);
		
		JMenuItem aboutOption = new JMenuItem("Om");
		helpMenu.add(aboutOption);
		saleWindow.getContentPane().setLayout(null);
		
		JPanel kundePanel = new JPanel();
		kundePanel.setBackground(SystemColor.inactiveCaption);
		kundePanel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Kunde", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		kundePanel.setBounds(10, 11, 261, 178);
		saleWindow.getContentPane().add(kundePanel);
		kundePanel.setLayout(null);
		
		JLabel customerIDLabel = new JLabel("Kunde ID:");
		customerIDLabel.setBounds(10, 48, 88, 14);
		kundePanel.add(customerIDLabel);
		
		JLabel orLabel = new JLabel("eller");
		orLabel.setBounds(10, 73, 46, 14);
		kundePanel.add(orLabel);
		
		JLabel phoneLabel = new JLabel("Telefonnr.:");
		phoneLabel.setBounds(10, 98, 76, 14);
		kundePanel.add(phoneLabel);
		
		JLabel typeInLabel = new JLabel("Indtast");
		typeInLabel.setBounds(10, 23, 46, 14);
		kundePanel.add(typeInLabel);
		
		customerIDField = new JTextField();
		customerIDField.setBounds(84, 45, 131, 20);
		kundePanel.add(customerIDField);
		customerIDField.setColumns(10);
		
		phoneNumberField = new JTextField();
		phoneNumberField.setColumns(10);
		phoneNumberField.setBounds(84, 95, 131, 20);
		kundePanel.add(phoneNumberField);
		
		JButton btnAccepter = new JButton("Accepter");
		btnAccepter.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				customer = personCtr.findCustomer(Integer.parseInt(customerIDField.getText()), phoneNumberField.getText());
				
				order = orderCtr.createOrder(customer, employee);
				customerNameField.setText("" + customer.getName());
			}
		});
		
		btnAccepter.setBounds(9, 123, 89, 23);
		kundePanel.add(btnAccepter);
		
		JButton customerClearButton = new JButton("Ryd");
		customerClearButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				customerIDField.setText("");
				phoneNumberField.setText("");
			}
		});
		customerClearButton.setBounds(138, 123, 89, 23);
		kundePanel.add(customerClearButton);
		
		JLabel customerNameLabel = new JLabel("Kunde navn:");
		customerNameLabel.setBounds(10, 153, 76, 14);
		kundePanel.add(customerNameLabel);
		
		customerNameField = new JTextField();
		customerNameField.setEditable(false);
		customerNameField.setColumns(10);
		customerNameField.setBounds(84, 150, 131, 20);
		kundePanel.add(customerNameField);
		
		JPanel varePanel = new JPanel();
		varePanel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Vare", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		varePanel.setBounds(10, 200, 261, 178);
		saleWindow.getContentPane().add(varePanel);
		varePanel.setLayout(null);
		
		JLabel productIDLabel = new JLabel("Vare ID:");
		productIDLabel.setBounds(10, 21, 46, 14);
		varePanel.add(productIDLabel);
		
		JButton continueTradeButton = new JButton("Fors\u00E6t handel");
		continueTradeButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				orderCtr.createSale(amountField.getText(), productIDField.getText(), productNameField.getText());
				String price = Double.toString(orderCtr.getPrice(productIDField.getText(), productNameField.getText()));
				String totalPrice = Double.toString(orderCtr.getTotalPrice());
				priceField.setText(price);
				totalField.setText(totalPrice);
				productIDField.setText("");
				productNameField.setText("");
				amountField.setText("");
				
			}
		});
		continueTradeButton.setBounds(10, 146, 122, 23);
		varePanel.add(continueTradeButton);
		
		JLabel totalLabel = new JLabel("Total:");
		totalLabel.setBounds(10, 121, 114, 14);
		varePanel.add(totalLabel);
		
		productIDField = new JTextField();
		productIDField.setColumns(10);
		productIDField.setBounds(84, 18, 131, 20);
		varePanel.add(productIDField);
		
		JLabel productNameLabel = new JLabel("Vare navn:");
		productNameLabel.setBounds(10, 46, 70, 14);
		varePanel.add(productNameLabel);
		
		JLabel amountLabel = new JLabel("Antal:");
		amountLabel.setBounds(10, 71, 114, 14);
		varePanel.add(amountLabel);
		
		productNameField = new JTextField();
		productNameField.setColumns(10);
		productNameField.setBounds(84, 43, 131, 20);
		varePanel.add(productNameField);
		
		amountField = new JTextField();
		amountField.setColumns(10);
		amountField.setBounds(84, 68, 131, 20);
		varePanel.add(amountField);
		
		JLabel priceLabel = new JLabel("Pris:");
		priceLabel.setBounds(10, 96, 70, 14);
		varePanel.add(priceLabel);
		
		JButton productClearButton = new JButton("Ryd");
		productClearButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				productIDField.setText("");
				productNameField.setText("");
				amountField.setText("");
			}
		});
		productClearButton.setBounds(142, 146, 89, 23);
		varePanel.add(productClearButton);
		
		priceField = new JTextField();
		priceField.setEditable(false);
		priceField.setColumns(10);
		priceField.setBounds(84, 93, 107, 20);
		varePanel.add(priceField);
		
		totalField = new JTextField();
		totalField.setEditable(false);
		totalField.setColumns(10);
		totalField.setBounds(84, 118, 107, 20);
		varePanel.add(totalField);
		
		JLabel crownLabel2 = new JLabel("Kr.");
		crownLabel2.setBounds(201, 121, 39, 14);
		varePanel.add(crownLabel2);
		
		JLabel crownLabel = new JLabel("Kr.");
		crownLabel.setBounds(201, 99, 39, 14);
		varePanel.add(crownLabel);
		
		JButton endTradeButton = new JButton("Afslut handel");
		endTradeButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			 try
			 {
				orderCtr.endSale();
				receiptGUI = new ReceiptGUI(order, employee, customer);
			 }
			 catch(Exception e2)
			 {
				 System.out.println("Order could not be inserted into DB: Don't believe this message, it's lying :S " +e2);
			 }
			}
		});
		endTradeButton.setBounds(20, 389, 114, 23);
		saleWindow.getContentPane().add(endTradeButton);
		
		JButton backButton = new JButton("Tilbage");
		backButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				saleWindow.setVisible(false);
				mainGUI = new MainGUI(employee);
			}
		});
		backButton.setBounds(144, 389, 89, 23);
		saleWindow.getContentPane().add(backButton);
		saleWindow.setVisible(true);
	}
}
